package fhq.hcmute.edu.vn.network;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Random;

import fhq.hcmute.edu.vn.config.AppConfig;
import fhq.hcmute.edu.vn.gui.NetworkDialog;
import fhq.hcmute.edu.vn.gui.ServerGUI;

public final class NetworkUtil {
	public static final int PASSWORD_LENGTH = 6;

	private NetworkUtil() {

	}

	private static int findFreePort() throws IOException {
		ServerSocket server = new ServerSocket(0);
		int port = server.getLocalPort();
		server.close();
		return port;
	}

	private static String generateRandomPassword(final int length) {
		Random random = new Random((new Date()).getTime());
		char[] values = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
				'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
				'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
				'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
				'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
				'W', 'W', 'Y', 'Z' };

		String out = "";
		for (int i = 0; i < length; i++) {
			int idx = random.nextInt(values.length);
			out += values[idx];
		}
		return out;
	}

	private static String getIPAddress() throws ClassNotFoundException,
			IOException {
		String ipAddress = null;
		ArrayList<NetworkCard> arrayList = NetworkUtil.getListNetwork();

		NetworkCard networkCard = AppConfig.getInstance().getNetworkCard();
		while (networkCard == null) {
			NetworkDialog networkDialog = new NetworkDialog(
					ServerGUI.getFrame(), true);
			networkDialog.setVisible(true);
			networkCard = AppConfig.getInstance().getNetworkCard();
		}
		for (int i = 0; i < arrayList.size(); i++) {
			if (arrayList.get(i).getDisplayName()
					.equals(networkCard.getDisplayName())) {
				ipAddress = arrayList.get(i).getIpAddress();
			}
		}
		return ipAddress;

	}

	public static ComputerInfos getServerInfo() throws IOException,
			ClassNotFoundException {
		InetAddress addr = InetAddress.getLocalHost();
		String hostname = addr.getHostName();
		String ipAddress = getIPAddress();
		int port = findFreePort();
		String password = generateRandomPassword(PASSWORD_LENGTH);
		ComputerInfos serverInfo = new ComputerInfos(hostname, ipAddress, port,
				password);
		return serverInfo;
	}

	public static ArrayList<NetworkCard> getListNetwork()
			throws SocketException {
		ArrayList<NetworkCard> listNetworks = new ArrayList<>();
		for (Enumeration<NetworkInterface> en = NetworkInterface
				.getNetworkInterfaces(); en.hasMoreElements();) {
			NetworkInterface intf = en.nextElement();
			Enumeration<InetAddress> enumIpAddr;
			for (enumIpAddr = intf.getInetAddresses(); enumIpAddr
					.hasMoreElements();) {
				String address = enumIpAddr.nextElement().toString();
				if (!address.contains(":")) {
					NetworkCard network = new NetworkCard(intf.getName(),
							intf.getDisplayName(), address);
					listNetworks.add(network);
				}
			}
		}
		return listNetworks;
	}
}
